package com.github.zxhTom.demo.jol;

import java.util.ArrayList;
import java.util.List;

public class VolatileAddMoreThread {
    static volatile Integer index = 0;

    public static void main(String[] args) throws InterruptedException {

        while (true) {
            index = 0;
            int m = 100;
            final int n = 100;
            List<Thread> threadList = new ArrayList<Thread>();
            for (int i = 0; i < m; i++) {
                Thread thread = new Thread(new Runnable() {
                    public void run() {
                        for (int i1 = 0; i1 < n; i1++) {
                            index++;
                        }
                    }
                });
                thread.start();
                threadList.add(thread);
            }
            for (Thread thread : threadList) {
                thread.join();
            }
            System.out.println(index);
            if (index < m * n) {
                System.out.println("线程出现不安全index="+index);
                break;
            }
        }
    }
}
